var BaseDao = require("../../base-dao/BaseDao");


//景区列表
exports.sceniclist = function (callback) {
    var sql = "select * from tbl_merchart_sceniclist where status=1";
    BaseDao.execQuery(sql, function (err, rows1) {
        var sql2 = "select * from tbl_merchart_gooddetail where sign=1 and status=1";
        BaseDao.execQuery(sql2, function (err, rows2) {
            callback(err, {rows1:rows1,rows2:rows2});
        })
    })
}

//景区商品列表

exports.shoplist = function (params,callback) {
    var sql="select d.*,g.type,m.starttime,m.endtime from tbl_merchart_gooddetail d left join " +
        "tbl_merchart_goods g on d.goodid = g.id " +
        "left join tbl_merchart_perorder m on m.gooddetailid = d.id  " +
        "where d.status=1 and g.status=1 and d.spotid='"+params.sid+"' order by d.createAt DESC";
    BaseDao.execQuery(sql,function (err,datas) {
        callback(err,datas);
    })
}



//景区所有列表
exports.allshop = function (spotid,callback) {
    var sql='select * from tbl_merchart_gooddetail d left join tbl_merchart_goods g on d.goodid = g.id where d.spotid="'+spotid+'" and d.status=1 and g.status=1;';
    BaseDao.execQuery(sql,function (err,datas) {
        callback(err,datas);
    })
}

//对应条件下商品列表
exports.findgoods = function (params,callback) {
    var t = '';
    var starttime = '';
    var endtime = '';
    if(params.type=='住宿' || params.type=='餐饮' || params.type=='会议' || params.type=='游玩'){
        t = " and g.type = '"+params.type+"'";
    }
    if(params.timeone){
        starttime = new Date(params.timeone);
    }
    if(params.startDate && params.endDate){
        starttime = new Date(params.startDate);
        endtime = new Date(params.endDate);
    }
    var sql="select d.*,m.starttime usestarttime,m.endtime useendtime from tbl_merchart_gooddetail d " +
        "left join tbl_merchart_perorder m on m.gooddetailid = d.id "+
        "left join tbl_merchart_goods g on d.goodid = g.id " +
        "where d.spotid='"+params.spotid+"' " +
        "and d.status=1 and g.status=1"+t;
    BaseDao.execQuery(sql,function (err,datas) {
        var sql_c = "SELECT SUM(m.count) usecount,m.gooddetailid gooddetailid FROM tbl_merchart_perorder m " +
            "LEFT JOIN tbl_merchart_gooddetail d ON d.id = m.gooddetailid GROUP BY m.gooddetailid"
        BaseDao.execQuery(sql_c,function (err,data_c) {
            for (var i = 0;i<datas.length;i++){
                var pdate = new Date(datas[i].usestarttime);
                var edate = new Date(datas[i].useendtime);
                pdate = new Date(pdate.getFullYear(), pdate.getMonth(), pdate.getDate());
                edate = new Date(edate.getFullYear(), edate.getMonth(), edate.getDate());
                var reg = /\d+/g;
                if(starttime){
                    starttime = new Date(starttime.getFullYear(), starttime.getMonth(), starttime.getDate());
                    var tempST = starttime.toString().match(reg);
                    tempST = new Date(tempST[0], parseInt(tempST[1]) - 1, tempST[2]);
                }
                if (endtime) {
                    endtime = new Date(endtime.getFullYear(), endtime.getMonth(), endtime.getDate());
                    var tempET = endtime.toString().match(reg);
                    tempET = new Date(tempET[0], parseInt(tempET[1]) - 1, tempET[2]);
                }
                if(pdate){
                    var tempP = pdate.toString().match(reg);
                    tempP = new Date(tempP[0], parseInt(tempP[1]) - 1, tempP[2]);
                }
                if(edate){
                    var tempE = edate.toString().match(reg);
                    tempE = new Date(tempE[0], parseInt(tempE[1]) - 1, tempE[2]);
                }

                if(tempP<=tempST<=tempE){
                    for(var j=0;j<data_c.length;j++){
                        if(data_c[j].gooddetailid == datas[i].id){
                            if(datas[i].goodcounts>0){
                                if(data_c[j].usecount>=datas[i].goodcounts){
                                    datas[i].isremained = false;
                                }else {
                                    datas[i].isremained = true;
                                }
                            }else{
                                datas[i].isremained = true;
                            }
                        }
                    }
                }else{
                    datas[i].isremained = true;
                }
            }
            callback(err,datas);
        })
    })
}

//添加景点
exports.addsceniclist = function (obj, callback) {
    var sql = 'INSERT INTO tbl_merchart_sceniclist SET ?';
    BaseDao.execQuery(sql, obj, function (err, data) {
        callback(err, data);
    })
}

//添加商品信息
exports.addticketlist = function (obj, callback) {
    var sql = 'INSERT INTO tbl_merchart_goods SET ?';
    BaseDao.execQuery(sql, obj, function (err, data) {
        callback(err, data);
    })
}

//获取票的详情信息
exports.ticketdetail = function (id, callback) {
    var c = '';
    if (id) {
        c = ' and d.id="' + id + '" ';
    }
    var sql="select d.*,g.type from tbl_merchart_gooddetail d left join " +
        "tbl_merchart_goods g on d.goodid = g.id " +
        "where d.status=1 and g.status=1 "+c+" order by d.createAt DESC";
    BaseDao.execQuery(sql, function (err, data) {
        callback(err, data);
    })
}

//确认预定后保存预定商品信息
exports.reservemessage = function (obj, callback) {
    var sql = "INSERT INTO tbl_merchart_perorder SET ?";
    BaseDao.execQuery(sql, obj, function (err, data) {
        callback(err, data)
    })
}

//更新订单信息
exports.updatereservemes = function (payid,payment, callback) {
    var sql = 'update tbl_merchart_payment set payment ="'+payment+'" where payid="'+payid+'" and status=1';
    console.log(sql);
    BaseDao.execQuery(sql, function (err, data2) {
        var sql2 = 'UPDATE tbl_merchart_perorder SET orderstatus=1  WHERE payid ="' + payid + '" and status=1';
        BaseDao.execQuery(sql2, function (err, data) {
            callback(err, {data2:data2,data:data})
        })
    })
}

//加入购物车
exports.savetocart = function (obj, callback) {
    var sql = "INSERT INTO tbl_merchart_cart SET ?";
    BaseDao.execQuery(sql, obj, function (err, data) {
        callback(err, data)
    })
}

//单个商品预定支付成功
exports.onlyreserve = function (obj, callback) {
    var sql = "INSERT INTO tbl_merchart_pay SET ?";
    BaseDao.execQuery(sql, obj, function (err, data) {
        callback(err, data)
    })
}

exports.upstatus = function (obj, callback) {
    var sql = "update tbl_merchart_perorder set orderstatus=3 where status=1 and orderno='" + obj.orderno + "'";
    BaseDao.execQuery(sql, function (err, data) {
        callback(err, data)
    })
}